﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorldConqueror.Libraries.Maths.Costants;
using WorldConqueror.Libraries.Maths.MathShapes;

namespace WorldConqueror.Libraries.Maths.Functions.Area
{
    public class SphericalPolygonArea : MathShapeArea<IMathSphericalPolygon>
    {
        public SphericalPolygonArea(IMathSphericalPolygon mathShape)
            : base(mathShape)
        {
        }

        public override double CalculateBase()
        {
            var previousCoordinate = MathShape.MathCoordinates.Last();
            var nextCoordinate = MathShape.MathCoordinates.ElementAt(1);

            var thisCoordinateIndex = 0;

            foreach(var thisCoordinate in MathShape.MathCoordinates)
            {
                
                MathShape.Area += (nextCoordinate.Longitude - previousCoordinate.Longitude) * Math.Sin(thisCoordinate.Latitude);

                thisCoordinateIndex++;
                previousCoordinate = thisCoordinate;
                nextCoordinate = MathShape.MathCoordinates.ElementAt(thisCoordinateIndex + 1);
                
            }

            MathShape.Area = MathShape.Area * (-1) * Math.Pow(EarthConstants.EarthRadius, 2) / 2;

            return MathShape.Area;
        }
    }
}
